﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls.Primitives;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Kokomo.Controls
{
	[TemplatePart(Name = ContentHostPartName, Type = typeof(FrameworkElement))]
	public class DocumentTabGroup : TabbedDocumentBase
	{
		public const string ContentHostPartName = "PART_ContentHost";

		static DocumentTabGroup()
		{
			DefaultStyleKeyProperty.OverrideMetadata(typeof(DocumentTabGroup), new FrameworkPropertyMetadata(typeof(DocumentTabGroup)));
			FocusManager.IsFocusScopeProperty.OverrideMetadata(typeof(DocumentTabGroup), new FrameworkPropertyMetadata(true));
		}

		public DocumentTabGroup()
		{
			//EventManager.RegisterClassHandler(typeof(TabbedDocumentView), Selector.SelectedEvent, (RoutedEventHandler) OnSelected);

			this.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);
		}

		protected override System.Windows.DependencyObject GetContainerForItemOverride()
		{
			return new DocumentTab();
		}

		protected override bool IsItemItsOwnContainerOverride(object item)
		{
			return (item is DocumentTab);
		}

		void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
		{
			this.UpdateContent();
		}

		internal int _group;

		#region Templating

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

			this.ContentHostPart = this.Template.FindName("PART_ContentHost", this) as FrameworkElement;
		}

		#region Parts

		private FrameworkElement _contentHostPart;
		protected FrameworkElement ContentHostPart
		{
			get { return this._contentHostPart; }
			set
			{
				this._contentHostPart = value;
				this._contentHostScrollViewer = value as ScrollViewer;
				this._contentHostDecorator = value as Decorator;
			}
		}

		private ScrollViewer _contentHostScrollViewer;
		private Decorator _contentHostDecorator;
		#endregion

		#endregion

		#region Content
		private void SetContent(UIElement content)
		{
			if (this._contentHostScrollViewer != null) this._contentHostScrollViewer.Content = content;
			else if (this._contentHostDecorator != null) this._contentHostDecorator.Child = content;
		}

		private void UpdateContent()
		{
			FrameworkElement content = null;
			object selectedItem = this.SelectedItem;
			if (selectedItem != null)
			{
				DependencyObject selectedDependencyObject = selectedItem as DependencyObject;
				if (selectedDependencyObject != null)
				{
					content = this.GetVisualTreeForItem(selectedDependencyObject);
				}
				else
				{
					DocumentTab container = (DocumentTab)this.ItemContainerGenerator.ContainerFromItem(this.SelectedItem);
					if (
						(container != null)
						)
					{
						content = this.GetVisualTreeForItem(container);
					}
				}
			}

			this.SetContent(content);
		}
		#endregion

		#region Selection

		protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
		{
			base.OnItemsChanged(e);

			this.EnsureSelectionIfPossible();
		}

		protected override void OnSelectionChanged(SelectionChangedEventArgs e)
		{
			base.OnSelectionChanged(e);

			object selectedItem = this.SelectedItem;
			if (selectedItem != null)
			{
				FrameworkElement container = this.ItemContainerGenerator.ContainerFromItem(selectedItem) as FrameworkElement;
				if (container != null)
					container.BringIntoView();
			}

			this.UpdateContent();
		}
		#endregion
	}
}
